from wxpyview.ui.component.common.basewidget import *

class Slider(Widget):
    def _getParentParam(self, meta_widget):
        stopping_point = ['Panel', 'Frame']       
        return ComponentLibrary.findFirstMatchedAncestor(meta_widget, stopping_point)
    
    def _setParams(self, meta_widget):
        dom = meta_widget.getDOM()
        if not dom:
            raise IOException
        
        self.params["parent"] = self._getParentParam(meta_widget)
        self.params["id"] = -1
        self.params["value"] = int(dom.getAttribute("value"))
        self.params["minValue"] = int(dom.getAttribute("min"))
        self.params["maxValue"] = int(dom.getAttribute("max"))
        self.params["pos"] = ComponentLibrary.getPosParam(dom)
        self.params["size"] = ComponentLibrary.getSizeParam(dom)      
        self.params["style"] = 0
        #self.params["validator"]
        self.params["name"] = dom.getAttribute("name")
       
        self.tick_interval = dom.getAttribute("tickInterval")
        self.tick_place = dom.getAttribute("tickPlace")
        self.display_labels = dom.getAttribute("displayLabels")
       
        self.tick_interval = ComponentLibrary.stringToInteger(self.tick_interval)
        if self.tick_interval:
            self.params["style"] |= wx.SL_AUTOTICKS

        self.tick_place = self.tick_place.lower()

        self.display_labels = self.display_labels.lower()
        if self.display_labels == "true":
            self.params["style"] |= wx.SL_LABELS
   
    def create(self, meta_widget):
        self._setParams(meta_widget)

        slider = wx.Slider(**self.params)
        if self.tick_interval: slider.SetTickFreq(self.tick_interval, 1)
        
        FontProperty.set(meta_widget, slider)
        
        return slider